package com.lora.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.lora.dto.EquipmentDto;
import com.lora.entity.Ups;
import com.lora.service.EquipmentService;
import com.lora.service.resolver.UpsResolver;
import com.lora.web.BaseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


/**
 * @author qiubo
 */
@Controller
public class DemoController {

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private UpsResolver upsResolver;

    @RequestMapping("/index")
    public String demo() {
        // equipmentService.process("1", "CEgYsCQ=");
        String data = "04032301030001003815d80103700956094e0955000000000000138913891389006400640064094e0951094a000f000e001013891389138900590059005a08ae08af08aa001400210000138813881388003500510004000200070000000200060000000000030000004800dc000000fa087708780001000000fa7ff803e827bc";
        EquipmentDto<Ups> upsDto = upsResolver.resolve("efefefef", data);
        upsResolver.save(upsDto.getEntity(), false);
        return "index";
    }

    @ResponseBody
    @RequestMapping("/test")
    public String test() {
        JSONObject obj = new JSONObject();
        String eqid;
        /**
         * 温湿度 ffffff100000ef87
         */
        /*eqid = "ffffff100000ef87";
        obj.put("data", "AQEAAADOAALpAWI=");
        equipmentService.process(eqid, obj.toJSONString());*/


        /**
         * 无线烟感
         */
        /*eqid = "ffffff100000648f";
        obj.put("data", "AgHY");
        equipmentService.process(eqid, obj.toJSONString());*/

        /**
         * 红外感应器
         */
        /*eqid = "ffffff100000c6fc";
        obj.put("data", "AgEBQCQ=");
        equipmentService.process(eqid, obj.toJSONString());*/

        /**
         * 三相电量仪
         */
        /*eqid = "ffffff100000f109";
        obj.put("data", "04032001033c5df35da65dc80063004c0021001b0000002400000232000600110003001700060006010c03a401e7138490c3000096750000000000000000000000005d3a");
        equipmentService.process(eqid, obj.toJSONString());*/

         /**
         * 开关量
         */
         /*eqid = "ffffff100000f10f";
         obj.put("data", "04032101031000000000000000000000000000000000e459");
         equipmentService.process(eqid, obj.toJSONString());*/

        /**
         * UPS
         */
        /*eqid = "ffffff100000f0f8";
        String data = "AQNwCVYJTglVAAAAAAAAE4kTiROJAGQAZABkCU4JUQlKAA8ADgAQE4kTiROJAFkAWQBaCK4IrwiqABQAIQAAE4gTiBOIADUAUQAEAAIABwAAAAIABgAAAAAAAwAAAEgA3AAAAPoIdwh4AAEAAAD6f/gD6Ce8";
        obj.put("data", data);
        equipmentService.process(eqid, obj.toJSONString());
        data = "AQRCAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGrzQ==";
        obj.put("data", data);
        equipmentService.process(eqid, obj.toJSONString());*/

        /**
         * 空调UPS数据
         */
        //要分三次发送
        eqid = "ffffff100000f10d";
        obj.put("data","04032301034000d1004a00000000008200000000000000000000051c025d0251000000000002000001f600a5ffec015e00960050002300b4000000e1001e0000002d000300001e42");
        equipmentService.process(eqid, obj.toJSONString());
        obj.put("data","0403230101090000000000000000004d12");
        equipmentService.process(eqid, obj.toJSONString());
        obj.put("data","0403230103020007f986");
        equipmentService.process(eqid, obj.toJSONString());

        /**
         * 红外人体活动探测器
         */
        /*eqid = "4f33c4d427cb1aac";
        obj.put("data","fa0201105cd0f5");
        equipmentService.process(eqid, obj.toJSONString());*/

        /**
         * 门磁感器数据
         */
        /*eqid="93ce2e63d4ef266b";
        obj.put("data","fa020300f0d0f5");
        equipmentService.process(eqid, obj.toJSONString());*/

        return "index";
    }

    public static void main(String[] args) {
        // 084818b024
        System.out.println(BaseUtil.base64ToHex("AQRCAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKvN"));
    }

}
